package com.maschinenstuermer.example.ejb3.facade.impl;

import java.util.List;

import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.maschinenstuermer.example.ejb3.entity.Company;
import com.maschinenstuermer.example.ejb3.facade.ExampleFacade;

@Stateless
@Remote
public class ExampleFacadeImpl implements ExampleFacade {
	@PersistenceContext(unitName="company")
	private EntityManager entityManager;
	
	@SuppressWarnings("unchecked")
	public List<Company> findCompaniesByName(String name) {
		Query query = entityManager.createNamedQuery(Company.FIND_BY_NAME_WITH_EMPLOYEES);
		query.setParameter("name", name);
		List<Company> result = query.getResultList();
		return result;
	}

	public Company makePersistent(Company company) {
		entityManager.persist(company);
		return company;
	}

	public void remove(final Company company) {
		final Company attachedCompany = entityManager.merge(company);
		entityManager.remove(attachedCompany);
	}
}
